<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class IncomingAcceptTransformer extends IncomingTransformer {
	
	private $table_direction = 'Incoming';
	
	public $accept_response_id_list = array();
	
	public function __construct($filename, $schema_filename, $table_direction = 'Incoming') {
		
		$this->table_direction = $table_direction;
		
		$xmlservice = new XMLService($filename, $schema_filename);

		$campaignaction_list = $xmlservice->get_node_children($xmlservice->doc, 'campaignaction');
		
		$has_authentication = (count($xmlservice->doc->getElementsByTagName('authentication'))
								&& $xmlservice->doc->getElementsByTagName('authentication')->item(0) !== null);
		
		$using_partner_authentication = Zend_Registry::get('partner.authentication');
								
		foreach ($campaignaction_list as $campaignaction):
		
			$class_name 						= $this->table_direction .  'AcceptResponse';
			$accept_response 					= new $class_name();
			
			$acceptid 							= $xmlservice->get_single_child($campaignaction, 'acceptid');
			$id		 							= $xmlservice->get_single_child($campaignaction, 'id');
			$action		 						= $xmlservice->get_single_child($campaignaction, 'action');
			$partnerid		 					= $xmlservice->get_single_child($campaignaction, 'partnerid');
			$partnername		 				= $xmlservice->get_single_child($campaignaction, 'partnername');
			
			$accept_response->AcceptID			= $acceptid;
			$accept_response->ID				= $id;
			$accept_response->Action			= $action;
			$accept_response->PartnerID			= $partnerid;
			$accept_response->PartnerName		= $partnername;
			
			$accept_response->save();
			$column_name = $this->table_direction . 'AcceptResponseID';
			$this->accept_response_id_list[] 	= $accept_response->$column_name;
			
			$class_name 								= $this->table_direction .  'AcceptResponseHost';
			$accept_response_host 						= new $class_name();
			$column_name 								= $this->table_direction . 'AcceptResponseID';
			$accept_response_host->$column_name			= $accept_response->$column_name;
			if ($table_direction == 'Incoming'):
				$accept_response_host->RemoteHostAddr	= isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ""; 
			else:
				$accept_response_host->RemoteURL		= isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ""; 
			endif;
			
			$accept_response_host->HttpResponseCode		= 200;
			$accept_response_host->save();
			
			$accept_authentication = null;

			if ($has_authentication):
					
				$authentication 				= $xmlservice->doc->getElementsByTagName('authentication')->item(0);					
				$username 						= $xmlservice->get_single_child($authentication, 'username');
				$password 						= $xmlservice->get_single_child($authentication, 'password');
				
				$class_name 					= $this->table_direction .  'AcceptAuthentication';
				$accept_authentication 			= new $class_name();
				$column_name 					= $this->table_direction . 'AcceptResponseID';
				$accept_authentication->$column_name 		= $accept_response->$column_name;
				$accept_authentication->Username 			= $username;
				if ($table_direction == 'Incoming'):
					$accept_authentication->PasswordMD5 		= md5($password);
				else:
					$accept_authentication->PasswordEncoded 	= EncryptUtils::encrypt_pass($password);
				endif;
				$accept_authentication->save();
				
			endif;
			
			if ($using_partner_authentication == true && $table_direction == 'Incoming'):
					
				if ($accept_authentication === null 
						|| PartnerAuthentication::is_valid_user_for_partner_id($partnerid, 
														$accept_authentication->Username, 
														$accept_authentication->PasswordMD5)
														== false):

					throw new Exception("PartnerID is invalid for the username and password provided");
															
				endif;
			endif;
			
		endforeach;
		
	}	
	
}

